﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ANTProductAssemble_ManageAssemble_AssembleStepAdd : System.Web.UI.Page
{
    private TAssembleStep ams;
    private DataRow dr;
    private DataTable amsdt
    {
        get
        {
            return (DataTable)ViewState["amsdt"];
        }
        set
        {
            ViewState["amsdt"] = value;
        }
    }

    public ANTProductAssemble_ManageAssemble_AssembleStepAdd()
    {
        ams = new TAssembleStep();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            amsdt = new DataTable();
            amsdt.Columns.Add(new DataColumn("PostName", Type.GetType("System.String")));
            amsdt.Columns.Add(new DataColumn("UserID", Type.GetType("System.String")));
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        dr = amsdt.NewRow();
        dr["PostName"] = this.TextBox2.Text.Trim();
        dr["UserID"] = this.TextBox3.Text.Trim();
        if (amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "' and UserID = '" + this.TextBox3.Text.Trim()+"'").Length == 0)
        {
            if (amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "'").Length > 0)
                amsdt.Select("PostName = '" + this.TextBox2.Text.Trim() + "'")[0].Delete();
            amsdt.Rows.InsertAt(dr, 0);
            Bindams();
        }
        else
        {
            string str = "已经在列表中";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Bindams()
    {
        this.GridView1.DataSource = amsdt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        amsdt.Select("PostName = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "' and UserID = '" + GridView1.Rows[e.RowIndex].Cells[1].Text + "'")[0].Delete();
        Bindams();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (amsdt.Rows.Count < 1)
        {
            string str = "请先添加关键工序信息!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
        else
        {
            string productSN = this.TextBox1.Text.Trim();
            DateTime dt = DateTime.Now;
            TAssembleStepInfo asi = new TAssembleStepInfo(productSN, dt, User.Identity.Name);
            if (ams.TAssembleStepItem_Insert(asi, amsdt))
            {
                this.TextBox1.Text = "";
                this.TextBox1.Focus();
                string str = productSN + " 提交成功!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
            else
            {
                string str = "提交失败，请联系相关人员!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }
    }
}
